<?php
session_start();

include_once "config/common.php";
include_once "model/upload.class.php";
include_once "model/uploadImg.class.php";

require_once('lib/Ecd.class.php');

const url = "http://www.etuocloud.com/gateway.action";
const app_key = 'KP3e3CxLk2eldCTDFM0KP3QDLVq3B9Wl';
const app_secret = 'wKhjBD1VtZkG4XWlVPIHW733u7kTPsfPCdQ3iwMuqaRCorqhlbV2KlN75mRvNUY1';
const format = 'xml';

//初始化
$ecd = new Ecd(url,app_key,app_secret,format);

function random($length = 6 , $numeric = 0) {
	PHP_VERSION < '4.2.0' && mt_srand((double)microtime() * 1000000);
	if($numeric) {
		$hash = sprintf('%0'.$length.'d', mt_rand(0, pow(10, $length) - 1));
	} else {
		$hash = '';
		$chars = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789abcdefghjkmnpqrstuvwxyz';
		$max = strlen($chars) - 1;
		for($i = 0; $i < $length; $i++) {
			$hash .= $chars[mt_rand(0, $max)];
		}
	}
	return $hash;
}
$_SESSION['send_code'] = random(4,1);
$smarty->assign('code',$_SESSION['send_code']);

//二级分类行业列表
//一级分类
$sql="companies_categories where level=1";
$res=$commonc->getList($sql);
//二级分类


//表单提交
    $table_name = "companies";
	//if($_POST['submit']=='注册'){
		//编辑状态先获得原内容
		//上传图片，生成缩略图
		if($_FILES['img']['name']!=''){
			//设置路径
			$folder="admin/upload/image/";
			$annexFolder = $folder."large/";
			$middleFolder = $folder."middle/";
			$smallFolder = $folder."small/";
			//删除旧图
			@unlink($annexFolder.$img);
			@unlink($middleFolder.$img);
			@unlink($smallFolder.$img);
			//上传新图
			$upimg = new uploadImg();
			$img = $upimg->upLoad('img',$annexFolder);
			//中图
			$upimg->smallImg($annexFolder.$img,$middleFolder);
			//小图
			$upimg->smallImg($annexFolder.$img,$smallFolder);
		}
		//上传图片，生成缩略图
		if($_FILES['businesslicense']['name']!=''){
			//设置路径
			//$folder= $GLOBALS['common']['imgPath']."upload/image/";
			$folder="admin/upload/test/";
			$annexFolder = $folder."large/";
			$middleFolder = $folder."middle/";
			$smallFolder = $folder."small/";
			//删除旧图
			@unlink($annexFolder.$businesslicense);
			@unlink($middleFolder.$businesslicense);
			@unlink($smallFolder.$businesslicense);
			//上传新图
			$upimg = new uploadImg();
			$businesslicense = $upimg->upLoad('businesslicense',$annexFolder);
			//中图
			$upimg->smallImg($annexFolder.$businesslicense,$middleFolder);
			//小图
			$upimg->smallImg($annexFolder.$businesslicense,$smallFolder);
		}
		
		//上传图片，生成缩略图
		if($_FILES['legalperson']['name']!=''){
			$folder="admin/upload/person/";
			$annexFolder = $folder."large/";
			$middleFolder = $folder."middle/";
			$smallFolder = $folder."small/";
			//删除旧图
			@unlink($annexFolder.$img);
			@unlink($middleFolder.$img);
			@unlink($smallFolder.$img);
			//上传新图
			$upimg = new uploadImg();
			$legalperson = $upimg->upLoad('legalperson',$annexFolder);
			//中图
			$upimg->smallImg($annexFolder.$legalperson,$middleFolder);
			//小图
			$upimg->smallImg($annexFolder.$legalperson,$smallFolder);
		}
		
		//插入数据库
		//省名称
		$data=array('title'=>$_POST['companyName'],'upid'=>$_POST['second'],'province'=>$_POST['province'],'cirty'=>$_POST['city'],'county'=>$_POST['town'],'img'=>$img,'businesslicense'=>$businesslicense,'legalperson'=>$legalperson,'regphone'=>$_POST['mobile'],'state'=>'1');

			if($_POST &&  $_POST['submit']){
				if($img!=""){
					$commonc->insert($table_name,$data);
					$id = $commonc->insert_id();
				}
				if($_POST['adminName']!=''){
				$datas=array('name'=>$_POST['adminName'],'pwd'=>md5($_POST['userpass']),'company_id'=>$id,'group_id'=>'2');
				$commonc->insert('admin',$datas);
				$href="register.php";
				echo $commonc->submitAlert('注册成功！请等待审核通知',$href);
				}
			}
		//查询数据分类
	   $ss="companies_categories where level=1";
	   $ress=$commonc->getList($ss, $key="*");
	   
	   if($_GET['action'] == 'getCategory'){
		$cid = $_POST['cid'];
		$where = ' companies_categories where upid = "' . $cid . '"';
		$list = $commonc->getList($where);;
		$html = '<option value="">--请选择--</option>';
		$error = 0;
		if(is_array($list['result'])){
			foreach($list['result'] as $k => $v){
				$html .= '<option value="'.$v['id'].'">'.$v['title'].'</option>';
			}
		}else{
			$error = 1;
		}
		$result = array('error' => $error , 'html' => $html);
		die(json_encode($result));
	}
$smarty->assign('ress',$ress['result']);
$smarty->assign('res',$res['result']);
$smarty->display('register.html');

?>